package com.example.spring.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.example.spring.model.Role;

@Repository
public class RoleDAOImpl implements RoleDAO{
	@Autowired
    private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory){
		this.sessionFactory = sessionFactory;
	}  
  
    @SuppressWarnings("unchecked")
	public List<Role> getRole(int id) {  
    	
    	Session session = sessionFactory.openSession();
		Transaction transaction = session.beginTransaction();
        List<Role> roleList = null;
        try{
        	Query query = session.createQuery("from Role r where r.id = :id");  
            query.setParameter("id", id);  
            roleList = query.list();  
        }
        catch(HibernateException e){
			e.printStackTrace();
			if(transaction != null){
				transaction.rollback();
			}
		}
		finally{
			if(session != null){
				session.close();
			}
		}
        return roleList;  
    	
    }  
}
